package cn.com.ccxe.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.com.ccxe.core.entity.Keyword;
import cn.com.ccxe.core.entity.MonitorType;
import cn.com.ccxe.dao.OperationStatus;
import cn.com.ccxe.service.KeywordBussiness;
import cn.com.ccxe.web.form.KeywordActionForm;

public class KeywordAction extends BasciAction {

	private static final long 		serialVersionUID = 1L;
	private static final Logger 	logger = LoggerFactory.getLogger(KeywordAction.class);
	private KeywordBussiness 		bussiness = new KeywordBussiness();

	@Override
	protected void dispatch(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		logger.info("--------MonitorKeywordAction.dispatch()------");
		KeywordActionForm actionForm = new KeywordActionForm(req);
		String method = actionForm.getMethod();
		if (actionForm.getUserInfo() == null) {
			logger.info("用户信息已过期，请重新登录！");
			resp.sendRedirect("/login.jsp");
			invalidateSession(req);
			return;
		}
		if (StringUtils.isEmpty(method)) {
			show(req, resp, actionForm);
			return;
		}
		if ("add".equals(method)) {
			add(req, resp, actionForm);
			return;
		}
		if ("addRelationship".equals(method)) {
			addRelationship(req, resp, actionForm);
			return;
		}
		if ("toAddRelationship".equals(method)) {
			toAddRelationship(req, resp, actionForm);
			return;
		}
		if ("update".equals(method)) {
			update(req, resp, actionForm);
			return;
		}
		if ("toUpdate".equals(method)) {
			toUpdate(req, resp, actionForm);
			return;
		}
		if ("del".equals(method)) {
			del(req, resp, actionForm);
			return;
		}
		if ("delMonitorType".equals(method)) {
			delMonitorType(req, resp, actionForm);
			return;
		}
		if ("delAllRelationship".equals(method)) {
			delAllRelationship(req, resp, actionForm);
			return;
		}
	}

	/**
	 * 显示所有的关键字
	 * 
	 * @param req
	 * @param resp
	 * @param actionForm
	 * @throws ServletException
	 * @throws IOException
	 */
	private void show(HttpServletRequest req, HttpServletResponse resp, KeywordActionForm actionForm)
			throws ServletException, IOException {
		List<Keyword> keywordList = bussiness.getAllKeyword();
		req.setAttribute("keywordList", keywordList);
		forward(req, resp, "/zebra/monitor/keyword/show.jsp");
	}

	/**
	 * 删除操作将根据Keyword的Id来执行删除操作。
	 * 
	 * @param req
	 * @param resp
	 * @param actionForm
	 * @throws IOException
	 * @throws ServletException
	 */
	private void del(HttpServletRequest req, HttpServletResponse resp, KeywordActionForm actionForm)
			throws ServletException, IOException {
		OperationStatus status = bussiness.del(actionForm.getId());
		switch (status) {
		case RELATIONSHIP_EMPTY:
			error(req, resp, "删除失败，还有关键字还引用了其他的条目，需将其清理掉");
			break;
		case SUCCESS:
			redirect(req, resp, getPath());
			break;
		default:
			error(req, resp, "操作失败");
			break;
		}
		return;
	}

	/**
	 * 接受修改请求，跳转到修改页面。
	 * 
	 * @param req
	 * @param resp
	 * @param actionForm
	 * @throws ServletException
	 * @throws IOException
	 */
	private void toUpdate(HttpServletRequest req, HttpServletResponse resp, KeywordActionForm actionForm)
			throws ServletException, IOException {
		logger.info("--------MonitorTypeAction.toUpdate()------");
		KeywordBussiness bussiness = new KeywordBussiness();
		Keyword keyword = bussiness.get(actionForm.getId());
		if (keyword != null) {
			req.setAttribute("keyword", keyword);
			forward(req, resp, "/zebra/monitor/keyword/update.jsp");
			return;
		} else {
			error(req, resp, "没有找到相应的记录！");
		}
	}

	/**
	 * 在进行修改操作时，可以修改关键字的内容
	 * 
	 * @param req
	 * @param resp
	 * @param actionForm
	 * @throws ServletException
	 * @throws IOException
	 */
	private void update(HttpServletRequest req, HttpServletResponse resp, KeywordActionForm actionForm)
			throws ServletException, IOException {
		KeywordBussiness bussiness = new KeywordBussiness();
		Keyword keyword = paddedKeyword(actionForm);
		if (!(keyword.getId() > 0)) {
			error(req, resp, "更新操作失败，请确认数据是否完整！");
			return;
		}
		OperationStatus status = bussiness.update(keyword);

		switch (status) {
		case SUCCESS:
			redirect(req, resp, getPath());
			break;

		default:
			error(req, resp, "更新操作失败，请确认操作步骤是否正确！");
			break;
		}
	}

	/**
	 * 添加关键字，如包含Type，还需添加与Type之间的关系
	 * 
	 * @param req
	 * @param resp
	 * @param actionForm
	 * @throws ServletException
	 * @throws IOException
	 */
	private void add(HttpServletRequest req, HttpServletResponse resp, KeywordActionForm actionForm)
			throws ServletException, IOException {
		KeywordBussiness bussiness = new KeywordBussiness();
		Keyword keyword = paddedKeyword(actionForm);
		if (StringUtils.isEmpty(keyword.getText())) {
			error(req, resp, "关键字不能为空");
			return;
		}
		OperationStatus status = bussiness.add(keyword);
		switch (status) {
		case SUCCESS:
			redirect(req, resp, getPath());
			break;

		default:
			error(req, resp, "添加操作失败，请确认操作步骤是否正确！");
			break;
		}
	}

	private void addRelationship(HttpServletRequest req, HttpServletResponse resp, KeywordActionForm actionForm)
			throws ServletException, IOException {
		KeywordBussiness bussiness = new KeywordBussiness();
		Keyword keyword = paddedKeyword(actionForm);
		if (keyword.getId() <= 0 || keyword.getMonitorTypes() == null || keyword.getMonitorTypes().size() == 0) {
			error(req, resp, "请确保数据完整！！！");
			return;
		}
		int result = bussiness.addRelationship(keyword.getId(), keyword.getMonitorTypes());
		susscess(req, resp, "成功插入" + result + "条数据！");
	}

	private void toAddRelationship(HttpServletRequest req, HttpServletResponse resp, KeywordActionForm actionForm)
			throws ServletException, IOException {
		KeywordBussiness keywordBussiness = new KeywordBussiness();
		List<MonitorType> monitorTypes = bussiness.getMonitorNothingWithKeyword(actionForm.getId());
		Keyword keyword = keywordBussiness.get(actionForm.getId());

		req.setAttribute("keyword", keyword);
		req.setAttribute("monitorTypeList", monitorTypes);
		forward(req, resp, "/zebra/monitor/keyword/addRelationship.jsp");
	}

	/**
	 * 删除关键字与类别之间的关系
	 * 
	 * @param req
	 * @param resp
	 * @param actionForm
	 * @throws ServletException
	 * @throws IOException
	 */
	private void delMonitorType(HttpServletRequest req, HttpServletResponse resp, KeywordActionForm actionForm)
			throws ServletException, IOException {
		KeywordBussiness bussiness = new KeywordBussiness();
		Keyword keyword = paddedKeyword(actionForm);
		if (!(keyword.getId() > 0 && actionForm.getMonitorTypeId() > 0)) {
			error(req, resp, "删除关系失败，请确认数据是否完整！");
			return;
		}
		OperationStatus status = bussiness.delRelationStatus(keyword.getId(), actionForm.getMonitorTypeId());
		switch (status) {
		case SUCCESS:
			redirect(req, resp, getPath());
			break;
		default:
			error(req, resp, "删除关键字与类别之间的关系操作失败，请确认操作步骤是否正确！");
			break;
		}
	}

	private void delAllRelationship(HttpServletRequest req, HttpServletResponse resp, KeywordActionForm actionForm)
			throws ServletException, IOException {

		KeywordBussiness bussiness = new KeywordBussiness();

		OperationStatus status = bussiness.delAllRelationship(actionForm.getId());

		switch (status) {
		case SUCCESS:
			susscess(req, resp, "删除所有关系成功！");
			break;
		default:
			error(req, resp, "已经有客户关注此关键字，不能删除！");
			break;
		}

	}

	public String getPath() {
		return "/zebra/monitor/keyword/monitorKeywordAction";
	}

	/**
	 * 
	 * @param actionForm
	 * @return
	 */
	private Keyword paddedKeyword(KeywordActionForm actionForm) {
		Keyword keyword = new Keyword();
		keyword.setId(actionForm.getId());
		keyword.setText(actionForm.getText());
		keyword.setMonitorTypes(actionForm.getMonitorTypes());
		return keyword;
	}
}
